from fastapi import APIRouter, Query, HTTPException
from orm.models import *
from pydantic import BaseModel, Field
from fastapi import status
from common.error_codes import ErrorCode
from typing import Optional

api_employee_create = APIRouter()


class EmployeeInfo(BaseModel):
    employee_name: str = Field(..., title="员工名称", description='员工名称')
    employee_hobby: Optional[str] = Field(...,
                                          title="员工爱好",
                                          description="员工爱好")
    department_id: int = Field(..., description="部门ID")


@api_employee_create.post('/v1/create',
                          status_code=status.HTTP_200_OK,
                          description="创建部门员工")
async def create_department_employee_info(employee: EmployeeInfo):
    #需要判断创建的员工是否已经存在
    if await Employee.filter(name=employee.employee_name).first():
        return {
            "code": ErrorCode.EmployeeAlreadyExists,
            "message":
            ErrorCode.get_error_message(ErrorCode.EmployeeAlreadyExists)
        }
    employee_res = await Employee.create(name=employee.employee_name,
                                         department_id=employee.department_id,
                                         hobby=employee.employee_hobby)

    return {
        "status": status.HTTP_200_OK,
        "message": ErrorCode.get_error_message(ErrorCode.success),
        "data": employee_res
    }
